﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;

namespace MultLanguage
{
    /// <summary>
    /// 多语言管理类
    /// </summary>
    public class MultLanguageManage
    {
        public static MultLanguageManage Instance = new MultLanguageManage();

        /// <summary>
        /// form 列表
        /// </summary>
        ArrayList formList = new ArrayList();

        /// <summary>
        /// userControl 列表
        /// </summary>
        ArrayList userControlList = new ArrayList();

        /// <summary>
        /// 注册FORM
        /// </summary>
        /// <param name="item"></param>
        public void RegForm(FormMultLangBase item)
        {
            if (!formList.Contains(item))
            {
                formList.Add(item);
            }
        }

        /// <summary>
        /// 注册UC
        /// </summary>
        /// <param name="item"></param>
        public void RegUC(UCMultLangBase item)
        {
            if (!userControlList.Contains(item))
            {
                userControlList.Add(item);
            }
        }

        /// <summary>
        /// 设置语言
        /// </summary>
        /// <param name="lg">国家/地区语言区域代码</param>
        public void SetLanguage(string code)
        {
            CultureInfo m_ci = new CultureInfo(code);
            Thread.CurrentThread.CurrentCulture = m_ci;
            Thread.CurrentThread.CurrentUICulture = m_ci;
            CallBackLanguage();
        }

        /// <summary>
        /// 遍历注册过的FORM，切换语言
        /// </summary>
        void CallBackLanguage()
        {
            //遍历所有Form，以切换其语言
            foreach (FormMultLangBase form in formList)
            {
                form.ApplyLanguage();
            }
            //遍历所有UC，以切换其语言
            foreach (UCMultLangBase uc in userControlList)
            {
                uc.ApplyLanguage();
            }
        }
    }
}
